﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using Brain;

namespace Brain.Editor
{

    public partial class HLSLBuilder : Form
    {
        public HLSLBuilder()
        {
            InitializeComponent();

            combo_Platform.Items.Add(TargetPlatform.Windows);
            combo_Platform.Items.Add(TargetPlatform.Xbox360);
            combo_Platform.SelectedItem = TargetPlatform.Windows;
        }

        private void button_Build_Click(object sender, EventArgs e)
        {
            if (combo_Platform.SelectedItem != null)
            {
                EffectContent content = new EffectContent()
                {
                    Identity = new ContentIdentity { SourceFilename = "myshader.fx" },
                    EffectCode = this.textBox_Effect.Text
                };

                EffectProcessor processor = new EffectProcessor();

                MyProcessorContext context = new MyProcessorContext((TargetPlatform)combo_Platform.SelectedItem);

                CompiledEffectContent compiledEffect = processor.Process(content, context);
                Byte[] effectCode = compiledEffect.GetEffectCode();
                string csharp = ArrayUtil.ConvertArrayToCSHarpCode<byte>(effectCode, "effect");
                this.textBox_Output.Text = csharp;
            }
        }
    }

    class MyLogger : ContentBuildLogger
    {
        public override void LogMessage(string message, params object[] messageArgs) { }
        public override void LogImportantMessage(string message, params object[] messageArgs) { }
        public override void LogWarning(string helpLink, ContentIdentity contentIdentity, string message, params object[] messageArgs) { }
    }
    class MyImporterContext : ContentImporterContext
    {
        public override string IntermediateDirectory { get { return string.Empty; } }
        public override string OutputDirectory { get { return string.Empty; } }

        public override ContentBuildLogger Logger { get { return logger; } }
        ContentBuildLogger logger = new MyLogger();

        public override void AddDependency(string filename) { }
    }
    class MyProcessorContext : ContentProcessorContext
    {
        private TargetPlatform platform = TargetPlatform.Windows;
        public override TargetPlatform TargetPlatform { get { return platform; } }
        public override GraphicsProfile TargetProfile { get { return GraphicsProfile.HiDef; } }
        public override string BuildConfiguration { get { return string.Empty; } }
        public override string IntermediateDirectory { get { return string.Empty; } }
        public override string OutputDirectory { get { return string.Empty; } }
        public override string OutputFilename { get { return string.Empty; } }

        public override OpaqueDataDictionary Parameters { get { return parameters; } }
        OpaqueDataDictionary parameters = new OpaqueDataDictionary();

        public override ContentBuildLogger Logger { get { return logger; } }
        ContentBuildLogger logger = new MyLogger();

        public override void AddDependency(string filename) { }
        public override void AddOutputFile(string filename) { }

        public override TOutput Convert<TInput, TOutput>(TInput input, string processorName, OpaqueDataDictionary processorParameters) { throw new NotImplementedException(); }
        public override TOutput BuildAndLoadAsset<TInput, TOutput>(ExternalReference<TInput> sourceAsset, string processorName, OpaqueDataDictionary processorParameters, string importerName) { throw new NotImplementedException(); }
        public override ExternalReference<TOutput> BuildAsset<TInput, TOutput>(ExternalReference<TInput> sourceAsset, string processorName, OpaqueDataDictionary processorParameters, string importerName, string assetName) { throw new NotImplementedException(); }

        public MyProcessorContext(TargetPlatform plat)
        {
            this.platform = plat;
        }
    }
}
 